package com.rerun.service.impl;

import com.rerun.entity.vo.UserInfoVo;
import com.rerun.enums.ResumeStateEnum;
import com.rerun.enums.ResumeStatusEnum;
import com.rerun.service.IEduBackgroundService;
import com.rerun.service.IResumeService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rerun.common.util.CommonUtil;

import com.rerun.entity.MemberUser;
import com.rerun.entity.Resume;
import com.rerun.entity.vo.JobIntentionVo;
import com.rerun.enums.DataFromEnum;
import com.rerun.enums.SexEnum;

import com.rerun.mapper.MemberUserMapper;
import com.rerun.mapper.ResumeMapper;
import com.rerun.service.IMemberUserService;
import com.rerun.vo.memberUser.StudentVo;

import javax.annotation.Resource;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * The type Member user service.
 *
 * @author Administrator 求职端用户登录接口
 */
@Service
public class MemberUserServiceImpl extends ServiceImpl<MemberUserMapper, MemberUser> implements IMemberUserService {

    @Resource
    private IEduBackgroundService iEduBackgroundService;
    @Resource
    private IResumeService iResumeService;
    @Resource
    private MemberUserMapper memberUserMapper;
    @Autowired
    private ResumeMapper resumeMapper;
//	@Autowired
//	private JobIntentionMapper jobIntentionMapper;


    @Override
    public Page<StudentVo> selectStudentPage(Page<StudentVo> page, StudentVo studentVo) {
        page.setRecords(this.baseMapper.selectStudentPage(page, studentVo));
        return page;
    }

    @Override
    public void memberUserTask(List<MemberUser> users) {
        boolean flag = insertBatch(users);
        if (flag) {
            iEduBackgroundService.updateMemberIdTask();
            iResumeService.updateMemberIdTask();
        }
    }

    @Override
    public MemberUser selectByPhone(String phone) {
        return memberUserMapper.selectByPhone(phone);
    }

    /**
     * 通过用户id获取 个人信息
     */
    @Override
    public JobIntentionVo selectMemberMessage(@Param(value = "id") String memberId) {

        JobIntentionVo jobIntention = memberUserMapper.selectMemberMessage(memberId);
        return jobIntention;

    }

    /**
     * 编辑 完善个信息
     */
    @Override
    public boolean updatePerfectMemberMessage(JobIntentionVo jobIntention) {

        String memberId = jobIntention.getMemberId();
        String sex = jobIntention.getSex();
        String headerImg = jobIntention.getHeaderImg();
        String name = jobIntention.getName();
        String email = jobIntention.getEmail();
        String highestEdu = jobIntention.getHighestEdu();
        String workStatus = jobIntention.getWorkStatus();
        /*
         *编辑 完善  个人信息
		 */
        try {
            EntityWrapper<MemberUser> wrapper = new EntityWrapper<MemberUser>();
            wrapper.eq("id", memberId);
            MemberUser user = memberUserMapper.selectById(memberId);
            if (sex != null) {
                int sex_int = 1;//男
                if (sex.equals(SexEnum.FEMALE.getDesc()) || sex.equals("0")) {
                    sex_int = 0;//女
                }

                user.setSex(sex_int);
            }
            user.setHeaderImg(headerImg);
            user.setEmail(email);
            user.setHighestEdu(highestEdu);
            memberUserMapper.update(user, wrapper);

            //memberId 存在 就更新   不存在就插入
            /*EntityWrapper<JobIntention > wrapper1=new EntityWrapper<JobIntention>();
            wrapper1.eq("memberId",memberId);
			JobIntention job = jobIntentionMapper.selectByMemberId(memberId);
			if(job!=null){
				job.setHighestEdu(highestEdu);
				job.setWorkStatus(workStatus);
				job.setUpdateTime(new Date());
				jobIntentionMapper.update(job, wrapper1);

			}else{
				JobIntention job1= new JobIntention();
				job1.setMemberId(user.getId());
				job1.setHighestEdu(highestEdu);
				job1.setWorkStatus(workStatus);
				job1.setUpdateTime(new Date());
				job1.setJobType("");
				job1.setCreateTime(new Date());
				jobIntentionMapper.insert(job1);
			}*/

            //memberId 存在 就更新   不存在就插入
            EntityWrapper<Resume> entitywrapper = new EntityWrapper<Resume>();
            entitywrapper.eq("memberId", memberId);
            Resume resume = resumeMapper.selectByMemberId(memberId);
            if (resume != null) {
                resume.setSex(user.getSex());
                resume.setHeaderImg(headerImg);
                resume.setName(name);
                resume.setUpdateTime(new Date());
                String birthday = jobIntention.getBirthday();
                if (birthday != null) {
                    Date date = CommonUtil.dateFormat(birthday);
                    resume.setBirthday(date);
                }
                resumeMapper.update(resume, entitywrapper);
            } else {
                Resume resume1 = new Resume();
                resume1.setMemberId(user.getId());
                resume1.setSex(user.getSex());
                resume1.setHeaderImg(user.getHeaderImg());
                resume1.setName(name);
                resume1.setPhoneNumber(user.getPhone());
                resume1.setUpdateTime(new Date());
                String birthday1 = jobIntention.getBirthday();
                if (birthday1 != null) {
                    Date date1 = CommonUtil.dateFormat(birthday1);
                    resume1.setBirthday(date1);
                }
                resume1.setPhoneNumber(user.getPhone());
                resume1.setDataFrom(DataFromEnum.SOCIAL.getType());
                resume1.setCreateTime(new Date());
                resumeMapper.insert(resume1);
            }
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public void updateUserInfo(UserInfoVo vo) {
        String memberId = vo.getMemberId();
        String img = vo.getHeaderImg();
        String name = vo.getName();
        long sex = vo.getSex();
        Date birthday = vo.getBirthday();
        String highestEdu = vo.getHighestEdu();
        String email = vo.getEmail();
        String livingCity = vo.getLivingCity();
        String residence = vo.getResidence();
        Date beginWork = vo.getBeginWork();
        MemberUser memberUser = memberUserMapper.selectById(memberId);

        if (Objects.nonNull(memberUser)) {
            memberUser.setHeaderImg(img);
            memberUser.setNickname(name);
            memberUser.setSex(sex);
            memberUser.setHighestEdu(highestEdu);
            memberUser.setEmail(email);
            memberUserMapper.updateById(memberUser);
        }

        String phone = memberUser.getPhone();
        Resume resume = iResumeService.selectByPhoneNumber(phone);
        if (Objects.nonNull(resume)) {
            resume.setUpdateTime(new Date());
            resume.setLivingCity(livingCity);
            resume.setResidence(residence);
            resume.setName(name);
            resume.setHeaderImg(img);
            resume.setBeginWork(beginWork);
            resume.setBirthday(birthday);
            iResumeService.updateById(resume);
        } else {
            resume = new Resume();

            resume.setMemberId(memberId);
            resume.setHeaderImg(img);
            resume.setName(name);
            resume.setSex(sex);
            resume.setBeginWork(beginWork);
            resume.setBirthday(birthday);
            resume.setCreateTime(new Date());
            resume.setStatus(ResumeStatusEnum.SHOW.getKey());
            resume.setResidence(residence);
            resume.setLivingCity(livingCity);
            resume.setDataFrom(DataFromEnum.SOCIAL.getType());
            resume.setPhoneNumber(phone);
            iResumeService.insert(resume);
        }
    }

    @Override
    public UserInfoVo getUserInfo(String memberId) {
        MemberUser memberUser = memberUserMapper.selectById(memberId);
        if (Objects.nonNull(memberUser)) {
            String phone = memberUser.getPhone();
            String email = memberUser.getEmail();
            String name = memberUser.getNickname();
            long sex = memberUser.getSex();
            String highestEdu = memberUser.getHighestEdu();
            String img = memberUser.getHeaderImg();

            UserInfoVo vo = new UserInfoVo();

            vo.setSex(sex);
            vo.setMemberId(memberId);
            vo.setHighestEdu(highestEdu);
            vo.setEmail(email);
            vo.setName(name);
            vo.setHeaderImg(img);

            Resume resume = iResumeService.selectByPhoneNumber(phone);
            if (Objects.nonNull(resume)) {
                Date birthday = resume.getBirthday();
                Date beginWork = resume.getBeginWork();
                String livingCity = resume.getLivingCity();
                String residence = resume.getResidence();
                String resumeId=resume.getId();
                vo.setResumeId(resumeId);
                vo.setBirthday(birthday);
                vo.setBeginWork(beginWork);
                vo.setLivingCity(livingCity);
                vo.setResidence(residence);
            }

            return vo;
        }
        return null;
    }
}
